import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.Texts;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;
import javax.xml.namespace.QName;
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFHeader;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.xmlbeans.XmlObject;

/**
 * <p>
 * 文本测试
 * </p>
 *
 * @Author: Liziba
 * @Date: 2021/6/24 21:49
 */
public class main {


    static String filePath = "C:\\Users\\jianfengw\\Desktop\\111.docx";
    static String targetPath = "C:\\Users\\jianfengw\\Desktop\\2222.docx";

    public static void main(String[] args) throws IOException {
//        test();
//        worldNews();
    }

    public static void test() throws IOException {
    }

    public void paramsReplace() throws IOException {
        XWPFTemplate compile = XWPFTemplate.compile(filePath);
//        List<MetaTemplate> elementTemplates = compile.getElementTemplates();
//        AbstractChartTemplateRenderPolicy policy = new AbstractChartTemplateRenderPolicy() {
//            @Override
//            public void doRender(ElementTemplate elementTemplate, Object o, XWPFTemplate xwpfTemplate) throws Exception {
//
//            }
//        };

        Map<String, Object> map = new HashMap<>();
        map.put("name", "测试");
        map.put("author", Texts.of("Liziba").color("000000").create());
        map.put("link", Texts.of("百度").link("https://baidu.com").create());
        map.put("anchor", Texts.of("anchortxt").anchor("appendix1").create());
        XWPFTemplate template = compile.render(map);
        template.writeAndClose(new FileOutputStream(targetPath));
    }

    public static void addWordWaterMark(String waterMarkName) throws Exception {
        InputStream input = new FileInputStream(filePath);
        XWPFDocument doc = new XWPFDocument(input);
        XWPFHeaderFooterPolicy headerFooterPolicy = doc.createHeaderFooterPolicy();
        //添加文字水印
        headerFooterPolicy.createWatermark(waterMarkName);
        XWPFHeader header = headerFooterPolicy.getHeader(XWPFHeaderFooterPolicy.DEFAULT);
        XWPFParagraph paragraph = header.getParagraphArray(0);
        paragraph.getCTP().newCursor();
        XmlObject[] xmlobjects = paragraph.getCTP().getRArray(0).getPictArray(0)
                .selectChildren(new QName("urn:schemas-microsoft-com:vml", "shape"));
        if (xmlobjects.length > 0) {
            com.microsoft.schemas.vml.CTShape ctshape = (com.microsoft.schemas.vml.CTShape) xmlobjects[0];
            //设置水印颜色
            ctshape.setFillcolor("#404040");
            //修改水印样式 //设置水印的样式  获取默认样式和旋315度
            ctshape.setStyle(getWaterMarkStyle(ctshape.getStyle(), 23) + ";rotation:315");
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            doc.write(out);
        } finally {
            out.close();
            doc.close();
            input.close();
        }
        out.writeTo(new BufferedOutputStream(new FileOutputStream(targetPath)));
    }


    public static String getWaterMarkStyle(String styleStr, double height) {
        Pattern p = Pattern.compile(";");
        String[] strs = p.split(styleStr);
        for (String str : strs) {
            if (str.startsWith("height:")) {
                String heightStr = "height:" + height + "pt";
                styleStr = styleStr.replace(str, heightStr);
                break;
            }
        }
        return styleStr;
    }


//    public static void worldNews() throws IOException {
//        Document document = Jsoup.connect("http://bjb.yunwj.top/php/60miao/qq.php").ignoreContentType(true).get();
//        JSONObject jsonObject = JSON.parseObject(document.text());
//        JSONArray wb = (JSONArray) jsonObject.get("wb");
//        for (Object newLine : wb) {
//            List<String> line = (List<String>) newLine;
//            System.out.println(line.get(0));
//            System.out.println(line.get(1));
//        }
//    }

}